<?php
//
//-------------------------------------------------
// | CreateTime: 2020/8/27 14:28
//+------------------------------------------------
// | Author: chenxueyuan
//+------------------------------------------------
// | Describe: ...
//+------------------------------------------------

namespace app\admin\controller;



class Admin extends Common
{
    public function add(){
        return view();
    }
    public function addhanddle(){
        if($this->request->isPost()){
            $post=$this->request->post();

            $validate= validate('Admin');
            if (!$validate->check($post)){
                $this->error($validate->getError(),'add');
            }
            $post['password']=md5($post['password']);
            $adminInsertResult=db('admin')->insert($post);
            If($adminInsertResult){
                $this->success('管理员添加成功','admin/admin/lst');
            }else{
                $this->error('管理员添加失败','admin/admin/lst');
            }
        }else{
            $this->redirect('admin/admin/lst');
        }
    }

    public function lst(){
        $admin_select=db("admin")->field('name,id')->paginate(3);
        $this->assign('admin_select',$admin_select);
        return view();
    }

    public function upd($id=''){

        $admin_find=db('admin')->find($id);
        if(!$admin_find){
            $this->redirect('admin/tags/lst');
        }else{
            $this->assign('admin_find', $admin_find);
            return view();
        }
        $admin_find->save();
    }
    public function updhanddle(){
        if($this->request->isPost()){
            $post=$this->request->post();
            $admin_find=db('admin')->find($post['id']);
            if(empty($admin_find)){
                $this->redirect('admin/lst');
            }else{
                if($admin_find['password']==md5($post['password_old'])){
                    $validate= validate('AdminUpd');
                    if (!$validate->check($post)){
                        $this->error($validate->getError(),'admin/lst');
                    }
                    $admin_update_result=db('admin')->update(['password'=>md5($post['password_new']),'id'=>$post['id']]);
                   if($admin_update_result){
                       $this->success('密码修改成功','admin/lst');
                   }else{
                       $this->error('密码修改成功','admin/lst');
                   }
                }else{
                    $this->error('原密码错误，请重试','admin/lst');
                }
            }
        }else{
            $this->redirect('admin/lst');
        }
    }
    public function del($id='')
    {
        if($id==1){
            $this->success('超级管理员不可以删除的','admin/lst');
        }
        $admin_del_result=db('admin')->delete($id);
        if ($admin_del_result){
            $this->success('管理员删除成功','admin/lst');
        }else{
            $this->success('管理员删除失败','admin/lst');
        }
    }
}